﻿using System;
using System.Collections.Generic;
using DG.Tweening;
using SuperScrollView;
using UnityEngine;
using UnityEngine.UI;
using WssUtils;

namespace JQMergeFramework
{
    public class UIRank:MonoBehaviour
    {
        public LoopListView2 mLoopListView;
        public Button btnDayRank, btnCityRank, btnFriendRank;
        public Transform line;


        [Header("====自己的排行信息====")] 
        public Text myRankTxt;
        public Image myHeadImg;
        public Text myNameTxt;
        public Text myScoreTxt;
     
        

        private Text dayRankTxt, cityRankTxt, friendRankTxt;
        
        private const int rankCount = 100;
   
        private List<RankData[]> allRank = new List<RankData[]>();

        private int openIndex = 0;

        private int tweenAnim = 7;

        private bool isPlayCutAnim = false;
        
        private void Awake()
        {
            TestRank();

            dayRankTxt = btnDayRank.GetComponent<Text>();
            cityRankTxt = btnCityRank.GetComponent<Text>();
            friendRankTxt = btnFriendRank.GetComponent<Text>();
        }


        void TestRank()
        {
            //TODO 排行逻辑处理
            int score = 9999;

            for (int j = 0; j < 3; j++)
            {
                RankData[] rankDataAry = new RankData[rankCount];
                for (int i = 0; i < rankCount; i++)
                {
                    int types = 0;
                    if (i < 3 )
                    {
                        types = i;
                    }
                    else
                    {
                        types =( i - 2) % 2 == 1 ? 3 : 4;
                    }

                    if (j == 0)
                    {
                        rankDataAry[i] = new RankData("DayRank"+(i+1), score/(i+1), i+1, types);
                    }
                    else if (j == 1)
                    {
                        rankDataAry[i] = new RankData("CityRank"+(i+1), score/(i+1), i+1, types);
                    }
                    else if (j == 2)
                    {
                        rankDataAry[i] = new RankData("FriendRank"+(i+1), score/(i+1), i+1, types);
                    }
                }
                allRank.Add(rankDataAry);
            }
        }

        
        private void Start()
        {
            UIManager.Instance.AddBtnListener(this.transform);
        }


        void OnBtnClick(Transform trans)
        {

            switch (trans.name)
            {
                case "BtnClose":
                    Close();
                    break;
                case "BtnDayRank":
                    CutRankSV(0);
                    break;
                case "BtnCityRank":
                    CutRankSV(1);
                    break;
                case "BtnFriendRank":
                    CutRankSV(2);
                    break;
            }
        }
        
        
        public void Open()
        {
            GetComponent<UIPopAnimate>().PopStart();
            CutRankSV(0);
        }
        
        
        
        private LoopListViewItem2 OmGetItemByIndex(LoopListView2 loopView, int index)
        {
            
            if (index < 0 || index > allRank[openIndex].Length)
            {
                return null;
            }
        
            RankData info = allRank[openIndex][index];
            if (info == null)
            {
                return null;
            }
        
            /*2018.11以后MonoBehaviour不能  new  直接赋值就可*/
            // LoopListViewItem2 item = new LoopListViewItem2();
            var item = loopView.NewListViewItem("RankItem");
            var itemInfo = item.GetComponent<RankItem>();
            itemInfo.Init(info, index);

            if (isPlayCutAnim && index < tweenAnim)
            {
                itemInfo.PlayMoveAnim(index*0.1F, (num) =>
                {
                    //停止播放动画
                    isPlayCutAnim = num !=tweenAnim;
                });
            }
            return item;
        }


        /// <summary>
        /// 切换排行榜
        /// </summary>
        /// <param name="index"></param>
        void CutRankSV(int index)
        {
            openIndex = index;
            line.DOKill();
            if (openIndex == 0)
            {
                line.DOMoveX(btnDayRank.transform.position.x, 0.2F);
            }
            else if (openIndex == 1)
            {
                line.DOMoveX(btnCityRank.transform.position.x, 0.2F);
            }
            else if (openIndex == 2)
            {
                line.DOMoveX(btnFriendRank.transform.position.x,0.2F);
            }
            
            dayRankTxt.color =  openIndex == 0 ? new Color32(255,209,16,255):new Color32(255,255,255,255);
            cityRankTxt.color =  openIndex == 1 ? new Color32(255,209,16,255):new Color32(255,255,255,255);
            friendRankTxt.color =  openIndex == 2 ? new Color32(255,209,16,255):new Color32(255,255,255,255);

            isPlayCutAnim = true;
      
            
            
            
            if (mLoopListView.ItemTotalCount>0)
            {
                mLoopListView.ResetListView();
                mLoopListView.RefreshAllShownItemWithFirstIndex(0);
            }
            else
            {
                mLoopListView.InitListView(allRank[openIndex].Length, OmGetItemByIndex); 
            }
            
            
            RefreshMyRankInfo();
        }


        /// <summary>
        /// 刷新自己的排行信息
        /// </summary>
        void RefreshMyRankInfo()
        {
            myRankTxt.text = "99+";
            myNameTxt.text = "MYNAME";
            myScoreTxt.text = "0";
        }

        void Close()
        {
            GetComponent<UIPopAnimate>().PopEnd();
        }

    }
}